<?php
namespace Home\Controller;
use Think\Controller;
use Think\Image;
class AgentController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}


     public function user(){
        $user_id = $_SESSION ["userInfo"]['user_id'];
        if(empty($user_id)){
            $this->error('信息错误');exit;
        }

        $search = I('search');
        if($search){
            $where['user_name|company_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }
        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $where['w.user_id'] = $user_id;
        $count = M('worlddetails')
                ->alias("w")
                ->join("left join pt_user as u on u.user_id=w.user_id")
                ->join("left join pt_business as b on b.user_id=w.user_id")
                ->where($where)
                ->count();
        $header = C('header')['user'];
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $seo = [1=>'百度PC','百度移动','360PC','360移动','搜狗PC','搜狗移动'];
        $list = M('worlddetails')
                ->alias("w")
                ->join("left join pt_user as u on u.user_id=w.user_id")
                ->field('u.user_name,w.seo,w.keyword,w.site,w.amount,w.price,u.update_time,w.rank,w.last_rank,w.price as charge,w.standard,w.logtime,u.status,w.user_id,w.id as detail')
                ->where($where)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        $status = ['优化中','欠费'];
        // var_dump($list);exit;
        foreach ($list as $k => $v) {
            $list[$k]['seo'] =  $seo[$v['seo']];
            $list[$k]['update_time'] = date("Y-m-d H:i:s",$v['update_time']);
            $list[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
            $list[$k]['status'] = $status[$v['status']];
            $list[$k]['user_id'] = '<a href="#" class="add" user_id='.$v['user_id'].'>日报表</a>';
            $list[$k]['detail'] = '<a href="#" class="detail" wid='.$v['detail'].'>详情</a>';
        }

        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }



    public function details(){
        $user_id = I('user_id');
        if(!$user_id){
            $this->ajaxreturn(array('state' => 2, 'data' => '信息错误'), 'json');
        }

        $where['w.user_id'] = $user_id;
        $list = M('worlddetails')
                ->alias("w")
                ->join("left join pt_user as u on u.user_id=w.user_id")
                ->field('u.user_id,u.user_name,w.keyword,w.logtime,w.standard,w.price,u.status')
                ->where($where)
                ->select();
        if($list){
            $n = 1;
            foreach ($list as $k => $v) {
                $list[$k]['user_id'] = $n;
                $list[$k]['standard'] = '排名消费';
                $list[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
                $list[$k]['status'] = date("Y-m-d H:i:s",$v['logtime']).'任务达标扣费';
                $n++;
            }
            $this->ajaxreturn(array('state' => 1, 'data' => $list), 'json');
        }else{
            $this->ajaxreturn(array('state' => 1, 'data' => '数据为空'), 'json');
        }
    }

}